﻿/*
     * ==============================================
     * BBScreenManager Responsibility:
     * =============================================
 * Track SpriteManager, BBGameScreen, BBMenu, GameWindow(Colour, Size, Title)
 * 
 * SpriteManager: this class will track all assets loaded inside gamw world (E.g.: 20 Bricks, 1 paddle, 1-3 Balls, etc)
 * BBMenu: this class will track menu items, colour of menu items, font, size of menu items, track navigation of menu location, selection
 * BBGameWindow (MAYBE): tracks screen size, bg colour, screen title
 * BBScreenManager: determine later....
 * 
*/

#region Using Statements

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using BrickBreaker.Gameplay;
using BrickBreaker.Menu;
using BrickBreaker.Core;

#endregion

namespace BrickBreaker
{
    public class BBGlobalController //: Microsoft.Xna.Framework.Game
    {
        #region Controllers
        //
            private BBGameController gameController;
            private BBMenuController menuController;
        //
        #endregion


        #region Object Construction
        //
            /// <summary>
            /// Creates a global controller.
            /// </summary>
            public BBGlobalController()
            {
                // Initialize the window
                InitGameWindow(800, 600);

                BBGlobalConfig.DisplayDebug = false;

                BBGlobalConfig.programState = ProgramState.MENU;

                gameController = BBGameController.Instance();
                //gameController.Start();

                menuController = new BBMenuController();
                BBGlobalConfig.keyboard = new BBKeyboard();
                BBGlobalConfig.keyboard.KeyPressedState = PressedState.SINGLE_PRESS;
            }
        //
        #endregion


        #region Private Methods
        //
            /// <summary>
            /// Initializes the game windows resolution.
            /// </summary>
            /// <param name="width">The width of the screen.</param> 
            /// <param name="height">The height of the screen.</param>
            private void InitGameWindow(int width, int height)
            {
                // If we aren't using a full screen mode, the height and width of the window can
                // be set to anything equal to or smaller than the actual screen size.
                if ((width <= GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width)
                     && (height <= GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height))
                {
                    BBGlobalConfig.WindowWidth = width;
                    BBGlobalConfig.WindowHeight = height;

                    BBGlobalConfig.graphics.PreferredBackBufferWidth = width;
                    BBGlobalConfig.graphics.PreferredBackBufferHeight = height;
                    BBGlobalConfig.graphics.IsFullScreen = false;
                    BBGlobalConfig.graphics.ApplyChanges();
                }
            }
        //
        #endregion


        #region Public Methods
        //
            /// <summary>
            /// Loads the content of all controllers.
            /// </summary>
            public void LoadContent()
            {
                gameController.LoadContent();
                menuController.LoadContent();
            }

            /// <summary>
            /// Updates which controller should take control.
            /// </summary>
            /// <param name="gameTime">Provides a snapshot of timing values.</param>
            public void Update(GameTime gameTime)
            {
                //if (isExiting)
                //{
                //    this.Exit();
                //}

                if (BBGlobalConfig.programState == ProgramState.MENU)
                {
                    BBGlobalConfig.keyboard.KeyPressedState = PressedState.SINGLE_PRESS;
                    menuController.Update(gameTime);
                }
                else if (BBGlobalConfig.programState == ProgramState.PLAY)
                {
                    BBGlobalConfig.keyboard.KeyPressedState = PressedState.LONG_PRESS;

                    //if (gameController.HasLostGame)
                    //{
                        // pass control back to menu controller and go to game over menu/screen
                    //}
                    //else if (gameController.HasWonGame)
                    //{
                        // pass control back to menu controller and go to You Win menu/screen
                    //}
                    //else
                    //{
                        gameController.Update(gameTime);
                    //}
                    
                }
            }

            /// <summary>
            /// Draws the current controller only.
            /// </summary>
            /// <param name="gameTime">Provides a snapshot of timing values.</param>
            public void Draw(SpriteBatch spriteBatch)
            {
                #region DRAW MENU
                //
                    if (BBGlobalConfig.programState == ProgramState.MENU)
                    {
                        BBGlobalConfig.keyboard.KeyPressedState = PressedState.SINGLE_PRESS; 
                        menuController.Draw(spriteBatch);
                    }
                //
                #endregion DRAW MENU
                //
                #region DRAW GAME MENU
                //
                    else if (BBGlobalConfig.programState == ProgramState.PLAY)
                    {
                        //BBGlobalConfig.keyboard.KeyPressedState = PressedState.LONG_PRESS; 
                        gameController.Draw(spriteBatch);
                    }
                //
                #endregion DRAW GAME MENU
            }

            /// <summary>
            /// Exits the game.
            /// </summary>
            /// <param name="theGame">The game which should exit.</param>
            public void ExitGame(Game theGame)
            {
                theGame.Exit();
            }
        //
        #endregion
    }// end class
}// end namespace





#region InitGameWindow Method


// Initializes the Game Window Size 
//public bool InitGraphicsMode(int iWidth, int iHeight, bool bFullScreen)
//{
// If we aren't using a full screen mode, the height and width of the window can
// be set to anything equal to or smaller than the actual screen size.
//if (bFullScreen == false)
//{
//    if ((iWidth <= GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width)
//        && (iHeight <= GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height))
//    {
//        graphics.PreferredBackBufferWidth = iWidth;
//        graphics.PreferredBackBufferHeight = iHeight;
//        graphics.IsFullScreen = bFullScreen;
//        graphics.ApplyChanges();
//        return true;
//    }
//}
//else
//{
//    // If we are using full screen mode, we should check to make sure that the display
//    // adapter can handle the video mode we are trying to set.  To do this, we will
//    // iterate thorugh the display modes supported by the adapter and check them against
//    // the mode we want to set.
//    foreach (DisplayMode dm in GraphicsAdapter.DefaultAdapter.SupportedDisplayModes)
//    {
//        // Check the width and height of each mode against the passed values
//        if ((dm.Width == iWidth) && (dm.Height == iHeight))
//        {
//            // The mode is supported, so set the buffer formats, apply changes and return
//            graphics.PreferredBackBufferWidth = iWidth;
//            graphics.PreferredBackBufferHeight = iHeight;
//            graphics.IsFullScreen = bFullScreen;
//            graphics.ApplyChanges();
//            return true;
//        }
//    }
//}
//return false;
//}

#endregion InitGameWindow Method



#region OLD CODE


//public static GraphicsDeviceManager graphics;     // graphics variable
        //public static ContentManager content;

        //public static SpriteBatch spriteBatch;
        //public static SpriteFont spriteFont;
        
        //public static GameState gameState;
        //public static BBKeyboard keyboard;
        
        //public static bool isExiting = false;

        //Menu screen_Menu;
        //BBGameController screen_Game;
        

        //public BBScreenManager()//Game game)
           // : base(game)
        //{
            //graphics = new GraphicsDeviceManager(this);

            //content = new ContentManager(Services);
            //content.RootDirectory = "Content";

            //gameState = GameState.Play;

            //screen_Menu = new Menu();
            //screen_Game = new BBGameController();
            //keyboard = new BBKeyboard();
        //}

        //protected override void LoadContent()
        //{
            //spriteBatch = new SpriteBatch(GraphicsDevice);
            //spriteFont = content.Load<SpriteFont>("SpriteFont1");

            //if statements
            //screen_Menu.UpdateTextPositioning();
        //}

        //protected override void Update(GameTime gameTime)
        //{
            //if (isExiting)
            //{
                //this.Exit();
            //}

            //keyboard.Update();

            //if (gameState == GameState.Menu)
            //{
            //    screen_Menu.Update(gameTime);
            //}
            //else if (gameState == GameState.Play)
            //{
                //screen_Game.Update(gameTime);
            //}

            //base.Update(gameTime);
        //}

        //protected override void Draw(GameTime gameTime)
        //{
            //spriteBatch.Begin();

            //if (gameState == GameState.Menu)
            //{
            //    screen_Menu.Draw(spriteBatch);
            //}
            //else if(gameState == GameState.Play)
            //{
                //screen_Game.Draw(gameTime);
            //}

        //    base.Draw(gameTime);
        //}

#endregion OLD CODE 
